package com.gxa.saas.admin.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.gxa.saas.admin.service.UserService;
import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.entity.vo.UsersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 总后台用户管理
 * @Author cowboy
 * @Date 2022/7/19 15:52
 * @Version 1.0
 */
@Api("用户管理模块")
@RestController
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    /**
     *
     * @param
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 16:41
     */
    @ApiOperation("用户无参查询")
    @PostMapping("/QueryUser")
    @ResponseBody
    public Map QueryUser(){
        JsonResult jsonResult = new JsonResult();
        List<UsersVO> userVO = this.userService.QueryUser();
        System.out.println("#####################################################33");
        System.out.println(userVO);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(userVO);
        return jsonResult.getMap();
//        return userVO;
    }

    /**
     * 实现关键词查询
     * @param keyword
	 * @param creationTime
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 20:10
     */
    @ApiOperation("用户条件查询")
    @PostMapping("/QueryUserByCondition")
    public Map QueryUserByCondition(String keyword,String creationTime){
        LocalDateTime time = null;
        if(creationTime.length()>0) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime.parse(creationTime, df);
        }
        List<UsersVO> userVO = this.userService.QueryUserByCondition(keyword, time);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(userVO);
        return jsonResult.getMap();
    }

    /**
     * 解封、禁用用户
     * @param userVO
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:12
     */
    @ApiOperation("解封、禁用用户")
    @PostMapping("/OperationUser")
    public Map OperationUser(@ModelAttribute @NotNull UsersVO userVO) {

        this.userService.UpdateUser(userVO);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }

    /**
     *
     * @param id
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:51
     */
    @ApiOperation("删除用户")
    @PostMapping("/DeleteUser")
    public Map DeleteUser(@RequestParam(value = "id",required = true) @NotNull Integer id) {

        this.userService.DeleteUser(id);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");

        return jsonResult.getMap();
    }
}
